/**
 * Copyright 2000 self.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 *
 * @author Ashin Wimalajeewa
 */
package self.amigo.elem;

import java.awt.*;
import java.util.*;

import org.w3c.dom.*;

import self.awt.*;
import self.awt.painter.*;
import self.gee.*;
import self.lang.*;
import self.xml.*;

public class WindowView extends ARectangularHandledElement {
	
	/**
	 * If the internal state of this class ever changes in such a way that it can't be defaulted,
	 * then the {@link #serialVersionUID} should be incremented to ensure serialized instances cleanly fail.  
	 */
	private static final long serialVersionUID = 1;
	
  final static public String TITLE_PROP = "title";
  final static public String MAINMENU_PROP = "mainMenu";
  final static public String TITLEBAR_BUTTONS_PROP = "titleBarButtons";
  final static public String TOOLBAR_TOOLS_COUNT_PROP = "toolBarToolsCount";

  final static private Font titleFont = new Font( "Arial", Font.BOLD, 12 );
//  final static private Font menuFont = new Font( "Arial", Font.PLAIN, 12 );
  final static private Font menuFont = new Font( "DialogInput", Font.PLAIN, 12 );

  private String  title = "Window title";
  private String  mainMenu = "File  Edit  View  Help";
  private boolean titleBarButtons = true;
  private int toolbarCount = 0;

  transient protected TitlePaintContext   titleContext = new TitlePaintContext();
  transient protected MenuPaintContext    menuContext = new MenuPaintContext();

  protected           TextPainter         textPainter;

  public WindowView() {
    fig.setBounds( 10, 10, 600, 300 );
    resetFrame();
//    PainterManager.registerOwner( this );
//    textPainter = (TextPainter) PainterManager.getPainterForOwner( this, TextPainter.class );
  }

  public void       setLayer( IDiagramLayer layerToDrawOn, boolean add ) {
    super.setLayer( layerToDrawOn, add );
    textPainter = (TextPainter) PainterManager.getPainterForOwner( layer, TextPainter.class );
  }

  // IEditableElement support
  public void       getProperties( Map store ) {
    store.put( TITLE_PROP, title );
    store.put( MAINMENU_PROP, mainMenu );
    store.put( TITLEBAR_BUTTONS_PROP, (titleBarButtons ? Boolean.TRUE: Boolean.FALSE) );
    store.put( TOOLBAR_TOOLS_COUNT_PROP, new Integer(toolbarCount) );
  }

  public void       setProperties( Map data ) {
    Boolean sysBtns = (Boolean) data.get( TITLEBAR_BUTTONS_PROP );
    titleBarButtons = sysBtns.booleanValue();
    Integer tools = (Integer) data.get( TOOLBAR_TOOLS_COUNT_PROP );
    toolbarCount = tools.intValue();
    title = (String) data.get( TITLE_PROP );
    mainMenu = (String) data.get( MAINMENU_PROP );
    resetFrame();
    layer.setDirty();
  }

  // IPersistentElement support
  public void readFrom( Node self, HashMap idObjLookUp ) throws DOMException {
    super.readFrom( self, idObjLookUp );
    String sysBtnsAsString = DomUtils.getElementAttribute( self, TITLEBAR_BUTTONS_PROP );
    titleBarButtons = BooleanUtils.toValue( sysBtnsAsString );
    String toolsAsString = DomUtils.getElementAttribute( self, TOOLBAR_TOOLS_COUNT_PROP );
    toolbarCount = Integer.parseInt( toolsAsString );
    title = DomUtils.getElementAttribute( self, TITLE_PROP );
    mainMenu = DomUtils.getElementAttribute( self, MAINMENU_PROP );
    resetFrame();
  }

  public void writeTo( Document doc, Element self, HashMap objIdLookup ) throws DOMException {
    super.writeTo( doc, self, objIdLookup );
    DomUtils.setElementAttribute( self, TITLE_PROP, title );
    DomUtils.setElementAttribute( self, MAINMENU_PROP, mainMenu );
    DomUtils.setElementAttribute( self, TITLEBAR_BUTTONS_PROP, "" + titleBarButtons );
    DomUtils.setElementAttribute( self, TOOLBAR_TOOLS_COUNT_PROP, "" + toolbarCount );
  }

  public void       paint( Graphics surface ) {
    if ( outlineOnly ) {
      surface.setColor(frameColor);
      surface.drawRect( fig.x, fig.y, fig.width, fig.height );
      return;
    }

    GraphicAttrs attrs = new GraphicAttrs( surface );
    surface.setColor( Color.lightGray );
//    surface.setColor( Color.white );
    surface.fillRect( fig.x, fig.y, fig.width, fig.height );
    surface.setColor(frameColor);
    surface.drawRect( fig.x, fig.y, fig.width, fig.height );
    paintTitleBar( surface );
    paintActionBars( surface );
    attrs.reset( surface );
  }

  private void paintTitleBar( Graphics surface ) {
    surface.setColor( Color.blue );
    surface.fillRect( fig.x+1, fig.y+1, fig.width-1, 20 );
    textPainter.paint( surface, titleContext, false );

    if ( titleBarButtons ) {
      for ( int cntr = 0, extra = 0; cntr < 3; cntr++ )  {
        if ( cntr == 2 ) extra += 1;
        surface.setColor( Color.gray );
        surface.fillRect( fig.x + fig.width - 48 + extra, fig.y+4, 12, 12 );
        surface.setColor( Color.black );
        surface.drawRect( fig.x + fig.width - 48 + extra, fig.y+4, 12, 12 );
        extra += 14;
      }
    }
  }

  private void paintActionBars( Graphics surface ) {
    int extraHeight = 21;
    if ( !StringUtils.isNullOrEmpty(mainMenu) ) {
      surface.setColor( Color.lightGray );
      surface.fillRect( fig.x+1, fig.y+extraHeight, fig.width-1, 20 );
      surface.setColor( Color.black );
      surface.drawLine( fig.x, fig.y + extraHeight + 20, fig.x + fig.width, fig.y + extraHeight + 20 );
      textPainter.paint( surface, menuContext, false );
      extraHeight += 21;
    }
    if ( toolbarCount > 0 ) {
      int xPos = fig.x+1, yPos = fig.y+extraHeight, width = fig.width-1, maxSize = fig.x + width;
      surface.setColor( Color.lightGray );
      surface.fillRect( xPos, yPos, width, 27 );
      for ( int cntr = 0, extra = 0; cntr < toolbarCount; cntr++ ) {
        if ( xPos + 1 + extra + 38 > maxSize )
          break;
        surface.setColor( Color.black );
        surface.drawRect( xPos + 1 + extra, yPos + 1, 36, 24 );
        extra += 38;
      }
      surface.drawLine( xPos, yPos + 27, fig.x + fig.width, yPos + 27 );
    }
  }

  protected void resetFrame() {
    titleContext.resetBounds( fig );
    menuContext.resetBounds( fig );
  }

  protected class TitlePaintContext extends ATextRenderInfoAdapter {
    private Rectangle bounds = new Rectangle();

    public TitlePaintContext() {
      format = ITextRenderInfo.TRIM_FORMAT;
    }

    public void resetBounds( Rectangle r ) {
      bounds.setBounds( r );
      bounds.x += 5;
      bounds.y += 2;
      bounds.width -= 50;
      bounds.height = 18;
    }

    public String     getText() {
      return title;
    }

    public Rectangle  getBounds() {
      return bounds;
    }

    public Font       getFont() {
      return titleFont;
    }

    public Color      getColor() {
      return Color.white;
    }
  }

  protected class MenuPaintContext extends ATextRenderInfoAdapter {
    private Rectangle bounds = new Rectangle();

    public MenuPaintContext() {
      format = ITextRenderInfo.TRIM_FORMAT;
    }

    public void resetBounds( Rectangle r ) {
      bounds.setBounds( r );
      bounds.x += 5;
      bounds.y += 22;
      bounds.width -= 10;
      bounds.height = 18;
    }

    public String     getText() {
      return mainMenu;
    }

    public Rectangle  getBounds() {
      return bounds;
    }

    public Font       getFont() {
      return menuFont;
    }

    public Color      getColor() {
      return Color.black;
    }
  }
}